package my.test.mediatorpattern.best;

/**
 * @author:许玉康
 * @Date: 2019/7/14 14:52
 */
public class Mediator extends AbstractMediator {

    //中介者最重要的方法
    @Override
    public void execute(String str, Object...objects) {
        if ("purchase.buy".equals(str)) {
            this.buyComputer((Integer) objects[0]);
        } else if("sale.sell".equals(str)) {
            this.sellComputer((Integer) objects[0]);
        } else if("sale.offsell".equals(str)) {
            this.offSell();
        } else if("stock.clear".equals(str)) {
            this.clearStock();
        }
    }

    //采购电脑
    private void buyComputer(int number) {
        int saleStatus = super.sale.getSaleStatus();
        if(saleStatus > 80) {
            System.out.println("采购IBM电脑：" + number + "台");
            super.stock.increase(number);
        } else {
            int buyNumber = number / 2;
            System.out.println("采购IBM电脑:" + buyNumber + "台");
        }
    }

    private void sellComputer(int number) {
        if(super.stock.getStockNumber() < number) {
            super.purchase.buyIBMcomputer(number);
        }
        super.stock.decrease(number);
    }

    private void offSell() {
        System.out.println("折价销售IBM电脑" + stock.getStockNumber());
    }

    private void clearStock() {
        super.sale.offSale();
        super.purchase.refuseBuyIBM();
    }
}
